package com.bdmc.device.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.device.domain.DevHealthData;
import com.bdmc.device.domain.bo.DevHealthDataBo;
import com.bdmc.device.domain.vo.DevHealthDataVo;
import com.bdmc.device.mapper.DevHealthDataMapper;
import com.bdmc.device.service.IDevHealthDataService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 手环健康数据Service业务层处理
 *
 * @author bdmc
 * @date 2023-07-31
 */
@Service
public class DevHealthDataServiceImpl extends ServicePlusImpl<DevHealthDataMapper, DevHealthData, DevHealthDataVo> implements IDevHealthDataService {

    @Override
    public DevHealthDataVo queryById(Long healthId){
        return getVoById(healthId);
    }

    @Override
    public TableDataInfo<DevHealthDataVo> queryPageList(DevHealthDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DevHealthData> lqw = buildQueryWrapper(bo);
        Page<DevHealthDataVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<DevHealthDataVo> mySelectPage(DevHealthDataBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<DevHealthDataVo> queryList(DevHealthDataBo bo) {
        LambdaQueryWrapper<DevHealthData> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<DevHealthData> buildQueryWrapper(DevHealthDataBo bo) {
        LambdaQueryWrapper<DevHealthData> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDataTime() != null, DevHealthData::getDataTime, bo.getDataTime());
        lqw.eq(StringUtils.isNotBlank(bo.getImei()), DevHealthData::getImei, bo.getImei());
        lqw.eq(StringUtils.isNotBlank(bo.getHeart()), DevHealthData::getHeart, bo.getHeart());
        lqw.eq(StringUtils.isNotBlank(bo.getTemperature()), DevHealthData::getTemperature, bo.getTemperature());
        lqw.eq(StringUtils.isNotBlank(bo.getSystolic()), DevHealthData::getSystolic, bo.getSystolic());
        lqw.eq(StringUtils.isNotBlank(bo.getDiastolic()), DevHealthData::getDiastolic, bo.getDiastolic());
        lqw.eq(StringUtils.isNotBlank(bo.getOx()), DevHealthData::getOx, bo.getOx());
        lqw.eq(StringUtils.isNotBlank(bo.getRespiratory()), DevHealthData::getRespiratory, bo.getRespiratory());
        lqw.eq(StringUtils.isNotBlank(bo.getTurnover()), DevHealthData::getTurnover, bo.getTurnover());
        lqw.eq(StringUtils.isNotBlank(bo.getAuto()), DevHealthData::getAuto, bo.getAuto());
        lqw.eq(StringUtils.isNotBlank(bo.getBattery()), DevHealthData::getBattery, bo.getBattery());
        lqw.eq(StringUtils.isNotBlank(bo.getBloodGlu()), DevHealthData::getBloodGlu, bo.getBloodGlu());
        lqw.eq(StringUtils.isNotBlank(bo.getBgState()), DevHealthData::getBgState, bo.getBgState());
        lqw.eq(StringUtils.isNotBlank(bo.getOnline()), DevHealthData::getOnline, bo.getOnline());
        lqw.eq(bo.getHrv() != null, DevHealthData::getHrv, bo.getHrv());
        lqw.eq(bo.getBarometer() != null, DevHealthData::getBarometer, bo.getBarometer());
        lqw.eq(bo.getAltitude() != null, DevHealthData::getAltitude, bo.getAltitude());
        return lqw;
    }

    @Override
    public Boolean insertByBo(DevHealthDataBo bo) {
        DevHealthData add = BeanUtil.toBean(bo, DevHealthData.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setHealthId(add.getHealthId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(DevHealthDataBo bo) {
        DevHealthData update = BeanUtil.toBean(bo, DevHealthData.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(DevHealthData entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public DevHealthDataVo getVoByImei(String imei) {
        LambdaQueryWrapper<DevHealthData> lqw = Wrappers.lambdaQuery();
        lqw.eq(DevHealthData::getImei,imei).orderByDesc(DevHealthData::getHealthId).last(" limit 1");
        return getVoOne(lqw);
    }
}
